package com.zretc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zretc.dao.OrdersMapper;
import com.zretc.dao.ScreeningMapper;
import com.zretc.entity.Screening;
import com.zretc.service.ScreeningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ScreeningServiceImpl implements ScreeningService {
    @Autowired
    private ScreeningMapper screeningMapper;
    @Autowired
    private OrdersMapper ordersMapper;

    @Override
    public boolean delete(String id) {
        Integer screeningId = Integer.valueOf(id);
        // 先判断该场次有没有订单
        List<String> ordersListByScreeningId = ordersMapper.getOrdersListByScreeningId(screeningId);
        if(ordersListByScreeningId.size() > 0){
            return false;
        }else{
            return screeningMapper.delete(screeningId)>0;
        }
    }

    @Override
    public boolean update(Screening screening) {
        // 先判断该场次有没有订单
        List<String> ordersListByScreeningId = ordersMapper.getOrdersListByScreeningId(screening.getScreeningId());
        if(ordersListByScreeningId.size() > 0){
            return false;
        }else{
            return screeningMapper.update(screening)>0;
        }
    }

    @Override
    public boolean save(Screening screening) {
        // 判断是否有冲突
        return screeningMapper.insert(screening)>0;
    }

    @Override
    public PageInfo<Screening> getScreeningList(Map<String, String> condition) {
        String num = condition.get("pageNum");
        if (num == null||"".equals(num)){
            num = "1";
        }
        Integer pageNum = Integer.valueOf(num);
        // 获取显示数量
        String size = condition.get("pageSize");
        if (size == null||"".equals(size)){
            size = "10";
        }
        Integer pageSize = Integer.valueOf(size);
        String startTime = condition.get("startTime");
        if("".equals(startTime)){
            condition.put("startTime",null);
        }
        String endTime = condition.get("endTime");
        if("".equals(endTime)){
            condition.put("endTime",null);
        }
        String filmId = condition.get("filmId");
        if("".equals(filmId)){
            condition.put("filmId",null);
        }
        String auditoriumsId = condition.get("auditoriumsId");
        if("".equals(auditoriumsId)){
            condition.put("auditoriumsId",null);
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Screening> screeningList = screeningMapper.getScreeningList(condition);
        PageInfo<Screening> pageInfo = new PageInfo<>(screeningList);

        return pageInfo;
    }
}
